package org.one.stone.soup.wiki.webcam;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import org.one.stone.soup.constants.TimeConstants;
import org.one.stone.soup.grfx.ImageFactory;
import org.one.stone.soup.wiki.client.WikiClient;
import org.one.stone.soup.wiki.client.WikiFileWriter;
public class WebCam implements WikiFileWriter{
private static Player player = null;
private FrameGrabbingControl fgc;
private WikiClient client;
private BufferedImage bufferedImage;
private String pageName;
public static void main(String[] args)
{
try{
if(args.length==1)
{
new WebCam( args[0],"WebCam",null,null,null );
}
else if(args.length==2)
{
new WebCam( args[0],args[1],null,null,null );
}
else if(args.length==3)
{
new WebCam( args[0],args[1],args[2],null,null );
}
else
{
new WebCam( args[0],args[1],args[2],args[3],args[4] );
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public WebCam(String url,String pageName,String driver,String userId,String password) throws Exception
{
this.pageName = pageName;
client = new WikiClient(url,userId,password);
startPlayer(driver);
while(true)
{
capture();
System.out.println("Image sent");
try{Thread.sleep(TimeConstants.MINUTE_MILLIS);}catch(Exception e){}
}
}
public void startPlayer(String deviceName) throws Exception
{
if(deviceName==null)
{
deviceName = "vfw:Microsoft WDM Image Capture (Win32):0";
}
CaptureDeviceInfo di = CaptureDeviceManager.getDevice( deviceName );
MediaLocator ml = di.getLocator();
player = Manager.createRealizedPlayer(ml);
player.start();
while(player.getState()!=Player.Started)
{
try{Thread.sleep(50);}catch(Exception e){}
}
fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
}
public void stopPlayer() throws Exception
{
player.close();
player.deallocate();
}
public void capture() throws Exception
{
// Grab a frame
Buffer buf = fgc.grabFrame();
// Convert it to an image
BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
Image img = btoi.createImage(buf);
while(img==null)
{
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
try{Thread.sleep(10);}catch(Exception e){}
}
bufferedImage = ImageFactory.createBufferedImage(img);
client.putImage( bufferedImage,pageName,"capture.png",null );
}
public void writeFileTo(OutputStream oStream) throws IOException
{
ImageIO.write(bufferedImage,"PNG",oStream);
oStream.flush();
}
}